package com.hitqz.robot.biz.service.impl;

import cn.hutool.extra.spring.SpringUtil;
import com.hitqz.robot.api.business.dto.RobotControlButtonDto;
import com.hitqz.robot.api.business.dto.RobotSendClientMsgDto;
import com.hitqz.robot.biz.service.RobotClientCmdService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.function.Predicate;

/**
 * @author kehanjiang
 * @date 2024/07/18
 */
@Service
@Slf4j
public class RobotClientCmdServiceImpl implements RobotClientCmdService {

	Predicate<RobotControlButtonDto> buttonPredicate = new Predicate<RobotControlButtonDto>() {
		@Override
		public boolean test(RobotControlButtonDto button) {
			//... 后续button相关校验逻辑
			return StringUtils.hasText(button.getAction());
		}
	};


	@Override
	public Boolean send(String clientId, RobotControlButtonDto button) {
		if (StringUtils.hasText(clientId) && buttonPredicate.test(button)) {
			RobotSendClientMsgDto msgDto= new RobotSendClientMsgDto();
			msgDto.setCmd(21000);
			msgDto.setClientId(clientId);
			msgDto.setData(button);
			SpringUtil.getApplicationContext().publishEvent(msgDto);
			return Boolean.TRUE;
		}
		return Boolean.FALSE;
	}


}